package com.siyuan.sns.boss.web.action;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.siyuan.sns.boss.domain.entity.Boss;
import com.siyuan.sns.boss.service.BossService;

@ParentPackage("character")
@Action("login")
@Result(name = "success", location = "success.jsp")
public class LoginAction extends ActionSupport implements SessionAware {
	private String username;
	private String password;

	private BossService bossService;

	private Map<String, Object> session;

	public void setBossService(BossService bossService) {
		this.bossService = bossService;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() {
		Boss boss = this.bossService.findByUsername(this.username);
		if (boss == null) {
			this.addActionMessage("该用户不存在");
		} else if (boss.getPassword().equals(this.password)) {
			session.put("bossId", boss.getId());
			return "success";
		} else {
			this.addActionMessage("用户名或密码不正确");
		}
		return "login";
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
